/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.VidiconRecord;
import me.zhengjie.modules.biz.service.VidiconRecordService;
import me.zhengjie.modules.biz.service.dto.VidiconRecordQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @date 2023-02-17
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "vidicon-record管理")
@RequestMapping("/api/vidiconRecord")
public class VidiconRecordController {

    private final VidiconRecordService vidiconRecordService;

    @Log("导出照片数据")
    @ApiOperation("导出照片数据")
    @GetMapping(value = "/picture/download")
//    @PreAuthorize("@el.check('vidiconRecord:list')")
    public void exportPhotoRecord(HttpServletResponse response, VidiconRecordQueryCriteria criteria) throws IOException {
        vidiconRecordService.download(vidiconRecordService.queryPicture(criteria), response);
    }

    @Log("导出视频数据")
    @ApiOperation("导出视频数据")
    @GetMapping(value = "/video/download")
//    @PreAuthorize("@el.check('vidiconRecord:list')")
    public void exportVideoRecord(HttpServletResponse response, VidiconRecordQueryCriteria criteria) throws IOException {
        vidiconRecordService.download(vidiconRecordService.queryVideo(criteria), response);
    }

    @GetMapping("/video")
    @Log("查询视频")
    @ApiOperation("查询视频")
//    @PreAuthorize("@el.check('vidiconRecord:list')")
    public ResponseEntity<Object> queryVideo(VidiconRecordQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(vidiconRecordService.queryVideo(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping("/picture")
    @Log("查询照片")
    @ApiOperation("查询照片")
//    @PreAuthorize("@el.check('vidiconRecord:list')")
    public ResponseEntity<Object> queryPicture(VidiconRecordQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(vidiconRecordService.queryPicture(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增vidicon-record")
    @ApiOperation("新增vidicon-record")
//    @PreAuthorize("@el.check('vidiconRecord:add')")
    public ResponseEntity<Object> createVidiconRecord(@Validated @RequestBody VidiconRecord resources) {
        return new ResponseEntity<>(vidiconRecordService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改vidicon-record")
    @ApiOperation("修改vidicon-record")
//    @PreAuthorize("@el.check('vidiconRecord:edit')")
    public ResponseEntity<Object> updateVidiconRecord(@Validated @RequestBody VidiconRecord resources) {
        vidiconRecordService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除vidicon-record")
    @ApiOperation("删除vidicon-record")
//    @PreAuthorize("@el.check('vidiconRecord:del')")
    public ResponseEntity<Object> deleteVidiconRecord(@RequestBody Integer[] ids) {
        vidiconRecordService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/analysisPhoto")
    @ApiOperation("解析海康抓拍图片数据")
    public ResponseEntity<Object> analysisPhoto(@RequestBody String data) {
        return new ResponseEntity<>(vidiconRecordService.analysisPhoto(data), HttpStatus.CREATED);
    }

    @PostMapping("/record-complete")
    @Log("录像完成vidicon-record")
    @ApiOperation("录像完成vidicon-record")
    @AnonymousAccess
//    @PreAuthorize("@el.check('vidiconRecord:edit')")
    public ResponseEntity<Object> completeVidiconRecord(@RequestBody String data) {
        JSONObject jsonObject = JSONUtil.parseObj(data);
        int id = jsonObject.getInt("id");
        return new ResponseEntity<>(id, HttpStatus.OK);
    }
}
